package com.atguigu.gmall;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @ClassName: MyProducer
 * @Description:
 * @Author: kele
 * @Date: 2021/3/20 16:55
 **/
public class MyProducer {


    public static  KafkaProducer<String, String> producer;

    static{
        //将方法赋值给属性，可以直接调用，不需要new对象
        producer = kafkapProducer();
    }

    public static  KafkaProducer<String, String> kafkapProducer() {

        Properties properties = new Properties();
        //配置kafka连接的属性，
        properties.put("bootstrap.servers","hadoop102:9092,hadoop103:9092,hadoop104:9092");
        properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        //创建一个生产者
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        return producer;

    }

    //使用生产者将数据发送到kafka
    public static void send (String topic,String value) {

        //发送数据
        producer.send(new ProducerRecord<>(topic,value));

    }
}
